import requests, re,json,os
from datetime import datetime,timezone,timedelta
# EPG for Fanmingming.com to 112114.com
def main():
    dt = datetime.now().strftime('%Y%m%d')
    # path = os.path.dirname(os.path.abspath(__file__))+f'/channel/tvmao_{dt}.xml'
    path = os.path.dirname(os.path.abspath(__file__))+f'/channel/e_epg{dt}.xml'
    try:
        os.remove(path)
    except:
    	pass    
    if not os.path.exists(path):
        # 发起 GET 请求
        # url="https://live.fanmingming.com/e.xml"
        # url="http://epg.51zmt.top:8000/cc.xml"
        url="https://gitee.com/taksssss/tv/raw/main/epg/112114.xml"
        response = requests.get(url)
        response.raise_for_status()  # 如果请求返回了错误码，则抛出异常

        # 将内容写入文件
        with open(path, 'w', encoding='utf-8') as f:
            f.write(response.text)  # 确保写入的是内容而不是 URL
    file_del()
# 多余文件处理
def file_del():
    # 删除旧文件（如果存在）
    # 获取当前日期
    current_date = datetime.now()
    # 计算未来两天的日期
    dt2 = (current_date + timedelta(days=1)).strftime('%Y%m%d')
    dt3 = (current_date + timedelta(days=2)).strftime('%Y%m%d')

    # 构造文件路径
    path2 = os.path.dirname(os.path.abspath(__file__)) + f'/channel/e_epg{dt2}.xml'
    path3 = os.path.dirname(os.path.abspath(__file__)) + f'/channel/e_epg{dt3}.xml'

    # 检查文件是否存在，如果存在则删除
    try:
        if os.path.exists(path2):
            os.remove(path2)

        if os.path.exists(path3):
            os.remove(path3)
    except Exception as e:
    	pass
if __name__ == '__main__':
    main()